package com.nccs.design.modo.filterchain;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuzengsheng
 * @program nssc-parent
 * @createTime 2021-11-17- 19:43:00
 * @description 设计模式-- 责任链模式
 */

public class FilterChain implements StudyPrepareFilter {


    private int pos = 0;

    private Study study;

    private List<StudyPrepareFilter> studyPrepareFilterList;

    public FilterChain(Study study) {
        this.study = study;
    }

    public void addFilter(StudyPrepareFilter studyPrepareFilter) {
        if (studyPrepareFilterList == null) {
            studyPrepareFilterList = new ArrayList<>();
        }

        studyPrepareFilterList.add(studyPrepareFilter);
    }

    @Override
    public void doFilter(PreparationList thingList, FilterChain filterChain) {
        // 所有过滤器执行完毕
        if (pos == studyPrepareFilterList.size()) {
            study.study();
            return;
        }
        StudyPrepareFilter studyPrepareFilter = studyPrepareFilterList.get(pos);
        pos++;
        studyPrepareFilter.doFilter(thingList, filterChain);
    }
}
